package com.fengye.school.model.convertors;

import com.fengye.school.model.school.enums.GradeType;
import com.fengye.school.model.school.enums.GradingType;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * 泛型指的是你在实体用的类型，第二个是指需要映射到数据库的类型对应的java中的类型
 */
@Converter(autoApply = true)
public class GradeTypeConverter implements AttributeConverter<GradeType, String> {


    @Override
    public String convertToDatabaseColumn(GradeType attribute) {
        return attribute.getName();
    }

    @Override
    public GradeType convertToEntityAttribute(String dbData) {
        for (GradeType type : GradeType.values()) {
            if (type.getName().equals(dbData)) {
                return type;
            }
        }
        return null;
    }
}